//package com.douqu.game.merge.netty;
//
//import com.douqu.game.merge.manager.ConnectCallback;
//import io.netty.bootstrap.Bootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioSocketChannel;
//import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
//import com.douqu.game.core.util.LogUtils;
//
//import java.nio.ByteOrder;
//
//
///**
//* Created by Administrator on 2016/11/9.
//*/
//public class NettyTCPClient {
//
//
//    private EventLoopGroup workerGroup;
//
//    private String host;
//
//    private int port;
//
//    private ConnectCallback callback;
//
//    public void start(String host, int port, ConnectCallback callback)
//    {
//        this.host = host;
//        this.port = port;
//        this.callback = callback;
//
//        workerGroup = new NioEventLoopGroup();
//
//        try {
//            Bootstrap bootstrap = new Bootstrap();
//            bootstrap.group(workerGroup);
//            bootstrap.channel(NioSocketChannel.class);
//            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
//            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
//
//                @Override
//                protected void initChannel(SocketChannel ch) throws Exception {
//                    ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN,Integer.MAX_VALUE,0,4,2,4,true));
//                    ch.pipeline().addLast(new NettyTCPClientHandler(callback));
//                }
//            });
//            // 发起异步连接操作
//            ChannelFuture future = bootstrap.connect(host, port).sync();
//
//            if(future.isSuccess())
//            {
////                LogUtils.info("TCP Client Start Success---------【" + host + ":" + port + "】");
//            }
//            else
//            {
////                LogUtils.info("TCP Client Start Failed---------【" + host + ":" + port + "】");
//                stop();
//                callback.connect(false);
//            }
//        } catch (Exception e) {
//            stop();
//            callback.connect(false);
//        }
//    }
//
//    public synchronized void stop()
//    {
////        LogUtils.info("TCP Client Close---------【" + host + ":" + port + "】");
//        workerGroup.shutdownGracefully();
//    }
//
//
//}
